<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>遍历兄弟节点</h1>
    <h2 onclick="doPrevious()">向前遍历</h2>
    <p>previousSibling属性</p>
    <h2 onclick="doNext()">向后遍历</h2>
    <p>nextSibling属性</p>
    <ul>
        <li>1</li>
        <li>2</li>
        <li id="li-me">我</li>
        <li>3</li>
    </ul>
    <script>
        let me = document.querySelector("#li-me");
        function doPrevious() {
            let node = me;
            while (true) {
                // 获取前节点，如果前节点为空，那么返回null
                node = node.previousSibling;
                if (node == null) {
                    break;
                }
                console.log(node);
            }
            
        }

        function doNext() {
            let node = me;
            while ((node = node.nextSibling) != null) {
                console.log(node);
            }
            
        }
    </script>
</body>
</html>